<template>
  <el-dialog v-model="visible" :title="title" width="1200" show-footer show-zoom resize :close-on-click-modal="false" :close-on-press-escape="false" @close="close">
    <template #default>
      <el-form ref="formRef" :rules="rules" :model="form" label-align="right" label-width="120" size="default">
        <el-form-item label="调拨单号" prop="location_code" v-if="handleType != 'add'">
          {{ form.backup_warehouse_order_no }}
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="第三方出库单" prop="shipment_id">{{ form.shipment_id }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="第三方入库单号" prop="send_shipment_id">{{ form.send_shipment_id }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调出仓" prop="out_warehouse_name">{{ form.out_warehouse_name }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调入仓" prop="in_warehouse_name">{{ form.in_warehouse_name }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <vxe-table :data="form.detail" :round="true" height="500">
        <vxe-column title="SKU">
          <template #default="{ row }" width="220">
            <div>SKU：{{ row.sku }}</div>
            <div>自定义SKU：{{ row.custom_sku }}</div>
            <div>渠道SKU：{{ row.to_seller_sku }}</div>
            <div>FNSKU/第三方SKU：{{ row.send_fn_sku }}</div>
          </template>
        </vxe-column>
        <vxe-column field="owner_name" title="货主">
          <template #default="{ row }">
            {{ row.owner_name || '--' }}
          </template>
        </vxe-column>
        <vxe-column field="name" title="产品名称" width="250">
          <template #default="{ row }">
            {{ row.sku_cn_title || '--' }}
          </template>
        </vxe-column>
        <vxe-column field="outstock_num" title="出库数量">
          <template #default="{ row }">
            {{ row.outstock_num || '--' }}
          </template>
        </vxe-column>
        <vxe-column title="入库数量">
          <template #default="{ row }">
            <el-input v-model="row.submit_in_warehouse_num" placeholder="入库数量" :disabled="handleType == 'out'" />
          </template>
        </vxe-column>
        <vxe-column title="已入库数量">
          <template #default="{ row }">
            {{ row.in_warehouse_num}}
          </template>
        </vxe-column>
        <vxe-column title="差异">
          <template #default="{ row }">
            {{ row.outstock_num - Number(row.submit_in_warehouse_num || 0)}}
          </template>
        </vxe-column>
        <vxe-column field="remark" title="备注">
          <template #default="{ row }">
            <el-input v-model="row.remark" placeholder="备注" />
          </template>
        </vxe-column>
      </vxe-table>
    </template>

    <template #footer>
      <template v-if="handleType == 'view'">
        <div class="flex justify-center py-2">
          <el-button type="primary" @click="close">关 闭</el-button>
        </div>
      </template>
      <div class="flex justify-center py-2" v-else>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="stockOut">收货确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="ComModal" lang="jsx">
import { getAdjustInDetail,outAdjustIn } from '../api/index'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage,ElMessageBox } from 'element-plus'

const visible = ref(false)
const loading = ref(false)
const formRef = ref(null)
const emits = defineEmits(['query'])

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
})
const { form, title, handleType } = toRefs(data)

const statusMap = new Map([
  ['add', '新增'],
  ['edit', '编辑'],
  ['view', '查看'],
  ['in', '入库'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const getDetail = async (id) => {
  try {
    const res = await getAdjustInDetail({
      id
    })
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}

const stockOut = async () => {
  let formData = {}
  formData.id = form.value.id
  const massageArray = []
  formData.detail = form.value.detail.map((i, index) => {
    if (!i.submit_in_warehouse_num) {
      massageArray.push(`第 ${index + 1} 条入库数量需要大于0`)
    }
    return {
      id: i.id,
      submit_in_warehouse_num: i.submit_in_warehouse_num
    }
  })
  if (massageArray && massageArray.length) {
    return ElMessageBox({
      title: '提示',
      message: <div>
        {massageArray.map(d => h('p', null, d))}
      </div>,
    })
  }
  try {
    const res = await outAdjustIn(formData)
    ElMessage.success(res.message)
    emits('query')
    close()
  } catch (e) {
    throw Error(e)
  }
}
const close = () => {
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
